package com.example.a14110100420.yuejian.WAdapter.picture;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.a14110100420.yuejian.R;
import com.example.a14110100420.yuejian.WActivity.picture.PictureDetailActivity;
import com.example.a14110100420.yuejian.Wbean.picture.PictureList;
import com.example.a14110100420.yuejian.Wviewholder.picture.PictureListViewholder;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by wanger on 2017/10/25.
 */

public class PictureListAdapter extends RecyclerView.Adapter {
    private Context context;
    private List list;
    private LayoutInflater layoutInflater;

    public PictureListAdapter(Context context,List list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.picturelist_adapter,parent,false);
        PictureListViewholder viewholder = new PictureListViewholder(view);
        return viewholder;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        PictureListViewholder pictureListViewholder = (PictureListViewholder) holder;
        PictureList pictureList = (PictureList) list.get(position);
        if (null == pictureList)
            return;

        final String shaitu_id = pictureList.getId();
        if (null == pictureList.getShaier())
            return;
        final String userid = pictureList.getUserid();
        final String username = pictureList.getShaier().getUser_name();
        final String userTouxiang = (String) pictureList.getShaier().getPhoto();
        final String fatieTime = pictureList.getUpdate_time();
        final String fatiePicture = pictureList.getThumb0();
        final String fatieDetail = pictureList.getTitle();
        int fatieCommentNum = pictureList.getComment().size();





        pictureListViewholder.xingming.setText(username);
        pictureListViewholder.fatieShijian.setText(pictureList.getUpdate_time());
        pictureListViewholder.fatieDetail.setText(pictureList.getTitle());
        pictureListViewholder.comment_Num.setText(""+fatieCommentNum+"");

        Picasso.with(context)
                .load("http://172.24.10.175/workout/Uploads/"+userTouxiang)
                .into(pictureListViewholder.touxiang);
        Picasso.with(context)
                .load("http://172.24.10.175/workout/Uploads/"+fatiePicture)
                .into(pictureListViewholder.fatieTupian);
        if(pictureListViewholder.touxiang.getDrawable()==null){
            pictureListViewholder.touxiang.setImageResource(R.drawable.wutouxiang);
        }

        pictureListViewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,"晒图详情",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, PictureDetailActivity.class);
                intent.putExtra("shaitu_id",shaitu_id);
                intent.putExtra("fatie_detail",fatieDetail);
                intent.putExtra("user_name",username);
                intent.putExtra("fatiepic",fatiePicture);
                intent.putExtra("userpic",userTouxiang);
                intent.putExtra("fatie_time",fatieTime);
                intent.putExtra("userid",userid);

                context.startActivity(intent);


            }
        });


    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
